(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
		keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			getSchools();
		});
	});
	function pagebtns(){
		check("user-cellphone",checkMobilePhone,"check");
		check("user-email",checkEmail,"check");
		checkPwd("user-password","user-confirm-password");
		check("user-alias",isNull,"null");
		check("user-usercode",isNull,"null");
		$("#user-add").click(function(){
			if(checkUser() && check2("check-fail")){
				addUser();
			}
		});		
	}
	function getSchools(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
    	var data = {
				cmd:"getSchoolByPage",
				model:"school",
				userKey:userKey,
				data:{
					currentpage : 1,
					pagesize : 10000,
				},
				remark:"",
		};
    	AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
    		var content = "";
			if(res.result == 1 && res.data != undefined && res.data != ""){
				var list = res.data.data;
				for(var i = 0; i < list.length; i++){
					content += "<option schoolcode = '"+list[i].schoolcode+"'>"+list[i].name+"</option>";
				}
			}else{
				content += "<option>--</option>";
			}
			$("#user-schoolcode").html(content);
		},null,null);
	}
	function addUser(){
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var mainType = $("#user-name",parent.parent.document).attr("usertype");
		var params = {};
		var param = {};
		params["alias"] = encodeURI($("#user-alias").val());	
		params["region"] = encodeURI($("#user-region").val());
		params["address"] = encodeURI($("#user-address").val());
		params["cellphone"] = $("#user-cellphone").val();
		params["email"] = encodeURI($("#user-email").val());		
		params["remark"] = encodeURI($("#user-remark").val());			
		params["usercode"] = encodeURI($("#user-usercode").val());
		params["password"] = encodeURI($("#user-password").val());
		params["usertype"] = encodeURI($("#user-usertype option:selected").val());
		params["schoolcode"] = $("#user-schoolcode option:selected").attr("schoolcode");		
		params["islock"] = true;
		params["mainType"] = mainType;
		for(var key in params){
			if(params[key] != ""){
				param[key] = params[key];
			}
		}
		var data = {
				cmd:"webRegister",
				model:"user",
				userKey:userKey,
				data:param,
				remark:"",						
		};
		processBegin("正在添加中···");
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			if(res.result == 1 && res.data != undefined && res.data != ""){
				processEnd("添加用户成功");
			}else{
				processError("添加用户失败");
			}
		},null,null);	
	}
	
	function checkUser(){
		if($("#user-usercode").val() == ""){
			$("#user-usercode-ex").html("不能为空");
			return false; 
		}else if($("#user-password").val() == ""){
			$("#user-password-ex").html("不能为空");
			return false; 
		}else if($("#user-confirm-password").val() == ""){
			$("#user-confirm-password-ex").html("不能为空");
			return false; 
		}else if($("#user-alias").val() == ""){
			$("#user-alias-ex").html("不能为空");
			return false; 
		}else{
			return true;
		}
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
	
})();
